//
//  DataManager.m
//  eGiftr
//
//  Created by Phien Tram on 9/3/13.
//  Copyright (c) 2013 eGiftr. All rights reserved.
//

#import "DataManager.h"
#import "UserWebService.h"
#import "WishWebService.h"
#import "FriendWebService.h"
#import "ServiceNameMapper.h"
#import "Constants.h"

@implementation DataManager

- (id)initUserRegister:(id)delegate userName:(NSString *)userName email:(NSString *)email password:(NSString *)password city:(NSString *)city fbId:(NSString *)fbId fbToken:(NSString *)fbToken isFbUser:(BOOL)isFbUser
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        _serviceName = [ServiceNameMapper userRegister];
        
        self.serviceManager = [UserWebService userRegister:self userName:userName email:email
                                                  password:password city:city fbId:fbId fbToken:fbToken isFbUser:isFbUser];
    }
    return self;
}

+ (id)userRegister:(id)delegate userName:(NSString *)userName email:(NSString *)email password:(NSString *)password city:(NSString *)city fbId:(NSString *)fbId fbToken:(NSString *)fbToken isFbUser:(BOOL)isFbUser
{
    return [[self alloc] initUserRegister:delegate userName:userName email:email password:password city:city fbId:fbId fbToken:fbToken isFbUser:isFbUser];
}

- (id)initLogin:(id)delegate isFbUser:(NSNumber *)isFbUser login:(NSString *)username password:(NSString *)password
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        _serviceName = [ServiceNameMapper login];
        
        self.serviceManager = [UserWebService login:self isFbUser:isFbUser login:username password:password];
    }
    return self;
}

+ (id)login:(id)delegate isFbUser:(NSNumber *)isFbUser login:(NSString *)username password:(NSString *)password
{
    return [[self alloc] initLogin:delegate isFbUser:isFbUser login:username password:password];
}

- (id)initLogout:(id)delegate userId:(NSString *)userId
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        _serviceName = [ServiceNameMapper logout];
        
        self.serviceManager = [UserWebService logout:self userId:userId];
    }
    return self;
}

+ (id)logout:(id)delegate userId:(NSString *)userId
{
    return [[self alloc] initLogout:delegate userId:userId];
}

- (id)initUpdateFbFriend:(id)delegate fbId:(NSString *)fbId fbToken:(NSString *)fbToken userId:(NSString *)userId appToken:(NSString *)appToken
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        _serviceName = [ServiceNameMapper updateFbFriend];
        
        self.serviceManager = [UserWebService updateFbFriend:self fbId:fbId fbToken:fbToken userId:userId appToken:appToken];
    }
    return self;
}

+ (id)updateFbFriend:(id)delegate fbId:(NSString *)fbId fbToken:(NSString *)fbToken userId:(NSString *)userId appToken:(NSString *)appToken
{
    return [[self alloc] initUpdateFbFriend:delegate fbId:fbId fbToken:fbToken userId:userId appToken:appToken];
}

- (id)initFindKeywords:(id)delegate query:(NSString *)query cate:(NSString *)cate appToken:(NSString *)appToken
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        _serviceName = [ServiceNameMapper findKeywords];
        
        self.serviceManager = [WishWebService findKeywords:self query:query cate:cate appToken:appToken];
    }
    return self;
}

+ (id)findKeywords:(id)delegate query:(NSString *)query cate:(NSString *)cate appToken:(NSString *)appToken
{
    return [[self alloc] initFindKeywords:delegate query:query cate:cate appToken:appToken];
}

- (id)initGetRecommendedKeywords:(id)delegate appToken:(NSString *)appToken
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        _serviceName = [ServiceNameMapper getRecommendedKeywords];
        
        self.serviceManager = [WishWebService getRecommendedKeywords:self appToken:appToken];
    }
    return self;
}

+ (id)getRecommendedKeywords:(id)delegate appToken:(NSString *)appToken
{
    return [[self alloc] initGetRecommendedKeywords:delegate appToken:appToken];
}

- (id)initGetWish:(id)delegate wishId:(NSString *)wishId appToken:(NSString *)appToken
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        _serviceName = [ServiceNameMapper getWishById];
        
        self.serviceManager = [WishWebService getWish:self wishId:wishId appToken:appToken];
    }
    return self;
}

+ (id)getWish:(id)delegate wishId:(NSString *)wishId appToken:(NSString *)appToken
{
    return [[self alloc] initGetWish:delegate wishId:wishId appToken:appToken];
}

- (id)initGetWishListByUserId:(id)delegate userId:(NSString *)userId appToken:(NSString *)appToken page:(NSNumber *)page
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        _serviceName = [ServiceNameMapper getWishListByUserId];
        
        self.serviceManager = [WishWebService getWishListByUserId:self userId:userId appToken:appToken page:page];
    }
    return self;
}

+ (id)getWishListByUserId:(id)delegate userId:(NSString *)userId appToken:(NSString *)appToken page:(NSNumber *)page
{
    return [[self alloc] initGetWishListByUserId:delegate userId:userId appToken:appToken page:page];
}

- (id)initSaveWishesByUserId:(id)delegate userId:(NSString *)userId keywords:(NSArray *)keywords appToken:(NSString *)appToken
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        _serviceName = [ServiceNameMapper saveWishesByUserId];
        
        self.serviceManager = [WishWebService saveWishesByUserId:self userId:userId keywords:keywords appToken:appToken];
    }
    return self;
}

+ (id)saveWishesByUserId:(id)delegate userId:(NSString *)userId keywords:(NSArray *)keywords appToken:(NSString *)appToken
{
    return [[self alloc] initSaveWishesByUserId:delegate userId:userId keywords:keywords appToken:appToken];
}

- (id)initGetFriendsByUserId:(id)delegate userId:(NSString *)userId appToken:(NSString *)appToken page:(NSNumber *)page
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        _serviceName = [ServiceNameMapper getFriendsByUserId];
        
        self.serviceManager = [FriendWebService getFriendsByUserId:self userId:userId appToken:appToken page:page];
    }
    return self;
}

+ (id)getFriendsByUserId:(id)delegate userId:(NSString *)userId appToken:(NSString *)appToken page:(NSNumber *)page
{
    return [[self alloc] initGetFriendsByUserId:delegate userId:userId appToken:appToken page:page];
}

#pragma mark - WebServiceManager delegate

- (void)webServiewManagerBeforeParsingData:(WebServiceManager *)manager data:(NSData *)data
{
    //TODO - to cache data
}

- (void)webServiceManagerFinishGetData:(WebServiceManager *)manager error:(NSError *)error
{
    if ([_delegate respondsToSelector:@selector(dataManagerFinishGetData:response:error:)])
    {
        [_delegate dataManagerFinishGetData:self response:manager.result error:error];
    }
}

@end
